package com.chinackts.customsdeclarations.dto;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lianhong
 * @since 2021/10/29
 */
public class CustomsDeclarationsListDto {

    private int total;

    private List<JSONObject> rows = new ArrayList<>();

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public List<JSONObject> getRows() {
        return rows;
    }

    public void setRows(List<JSONObject> rows) {
        this.rows = rows;
    }

    public void addRows(List<JSONObject> rows) {
        if (CollectionUtils.isEmpty(rows)) return;
        this.rows.addAll(rows);
    }

    /**
     * 报关单 数据合并，使用entryId判断
     *
     * @param secondary 报关单
     */
    public CustomsDeclarationsListDto union(CustomsDeclarationsListDto secondary) {
        //次要的报关单列表
        List<JSONObject> secondaryRows = secondary.getRows();
        //主要报关单号列表
        List<String> mainEntryIds = this.rows.stream().map(item -> item.getString("entryId")).collect(Collectors.toList());
        //删除（已存在于主要报关单列表中的）次要报关单数据
        secondaryRows.removeIf(item -> mainEntryIds.contains(item.getString("entryId")));
        this.rows.addAll(secondaryRows);
        this.total = this.rows.size();
        return this;
    }
}
